﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Chenxi
{
    public class ConverterUIUtil
    {
        public delegate void OnFileSelected(string filePath);
        public static void InitUI(Form f, Label lName, Label lVersion)
        {
            f.Text = ConverterInfo.Name + " " + ConverterInfo.VersionName;
            lName.Text = ConverterInfo.Name;
            lVersion.Text = "Ver " + ConverterInfo.VersionName;
        }

        public static void BindFileSelect(Button b, TextBox t, FileDialog d, OnFileSelected afterFileSelected = null)
        {
            b.Click += new EventHandler(delegate (object sender, EventArgs e)
            {
                if (d.ShowDialog() == DialogResult.OK)
                {
                    t.Text = d.FileName;
                    if (afterFileSelected != null)
                        afterFileSelected(d.FileName);
                }
                    
            });
        }

        public static void BindFileDrag(TextBox t, OnFileSelected afterFileSelected = null)
        {
            t.AllowDrop = true;
            t.DragEnter += new DragEventHandler(delegate (object sender, DragEventArgs e) {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            });
            t.DragDrop += new DragEventHandler(delegate (object sender, DragEventArgs e) {
                string[] files = e.Data.GetData(DataFormats.FileDrop, false) as string[];
                if (files.Length >= 1)
                {
                    t.Text = files[0];
                    if (afterFileSelected != null)
                        afterFileSelected(files[0]);
                }
                    
            });

        }

        public static void ShowException(Exception e)
        {
            MessageBox.Show("Exception:\r\n" + e.ToString(), "转换时发生异常");
        }
    }
}
